package co.fitstart.entity.user.agent;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.demo2do.core.format.DecimalFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "agent_discount")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class AgentDiscount {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 折扣类型 */
    private DiscountType discountType;
    
    /** 折扣金额 */
    @DecimalFormat
    private Integer discountOffer;
    
    /** 回扣 */
    @DecimalFormat
    private Integer commission;
    
    /** 代言人折扣 */
    private Integer agentDiscount;
    
    /** 用户折扣 */
    private Integer userDiscount;
    
    /** 回扣百分比 */
    private Integer commissionPercent;
    
    /** 优惠券码 */
    private String discountCode;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Agent agent;
    
    /**
     * The default constructor
     */
    public AgentDiscount() {
        
    }
    
    /**
     * 
     * @param agent
     * @param discountOffer
     * @param commission
     */
    public AgentDiscount(Agent agent, DiscountType discountType, int discount, int offer) {
        this.discountType = discountType;
        this.agent = agent;
        this.disabled = false;
        if (this.discountType.equals(DiscountType.PAYMENT)) {
            this.discountOffer = discount;
            this.commission = offer;
        } else if (this.discountType.equals(DiscountType.DISCOUNT)) {
            this.agentDiscount = discount;
            this.userDiscount = offer;
            this.commissionPercent = offer - discount;
        }
        this.discountCode = String.valueOf(System.identityHashCode(this));
    }
    
    /**
     * 
     * @param price
     * @return
     */
    public int calculatePrice(int price) {
    	
    	BigDecimal realPrice = new BigDecimal(price);
    	
    	if (this.discountType.equals(DiscountType.PAYMENT)) { 
    		realPrice = realPrice.subtract(new BigDecimal(discountOffer));
    	} else {
    		realPrice = realPrice.multiply(new BigDecimal(userDiscount).divide(new BigDecimal(100)));
    	}
    	
    	return realPrice.intValue();
    }
    
    /**
     * 
     * @param price
     * @param originalPrice
     * @return
     */
    public int calculatePrice(int price, int originalPrice) {
        
        BigDecimal realPrice = new BigDecimal(price);
        
        if (this.discountType.equals(DiscountType.PAYMENT)) { 
            realPrice = realPrice.subtract(new BigDecimal(discountOffer));
        } else {
            BigDecimal discountPrice = new BigDecimal(originalPrice).multiply(new BigDecimal(100-userDiscount).divide(new BigDecimal(100)));
            realPrice = realPrice.subtract(discountPrice);
        }
        
        return realPrice.intValue();
    }
    
    /**
     * 
     * @param price
     * @return
     */
    public int calculateCommission(int price) {
    	
    	BigDecimal realPrice = new BigDecimal(price);
    	
    	if (this.discountType.equals(DiscountType.PAYMENT)) {
    		return commission;
    		
    	} else {
    		return realPrice.multiply(new BigDecimal(commissionPercent)).divide(new BigDecimal(100)).intValue();
    	}
    	
    }
    
    /**
     * 
     * @param price
     * @return
     */
    public Map<String, Object> describe(int price) {
        int discount = 0;
        
        BigDecimal realPrice = new BigDecimal(price);
        
        if (this.discountType.equals(DiscountType.PAYMENT)) {
            discount = price-discountOffer > 0 ? discountOffer : price-1;
            
        } else {
            int originalDiscount = realPrice.multiply(new BigDecimal(100-userDiscount)).divide(new BigDecimal(100)).intValue();
            discount =  price-originalDiscount > 0 ? originalDiscount : price-1;
        }
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("discount", discount);
        parameters.put("discountCode", discountCode);
        
        return parameters;
    }
    
    /**
     * 
     * @return
     */
    public AgentDiscount disable() {
        this.disabled = true;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the discountType
     */
    public DiscountType getDiscountType() {
        return discountType;
    }

    /**
     * @return the discountOffer
     */
    public Integer getDiscountOffer() {
        return discountOffer;
    }

    /**
     * @return the commission
     */
    public Integer getCommission() {
        return commission;
    }

    /**
     * @return the agentDiscount
     */
    public Integer getAgentDiscount() {
        return agentDiscount;
    }

    /**
     * @return the userDiscount
     */
    public Integer getUserDiscount() {
        return userDiscount;
    }

    /**
     * @return the commissionPercent
     */
    public Integer getCommissionPercent() {
        return commissionPercent;
    }

    /**
     * @return the discountCode
     */
    public String getDiscountCode() {
        return discountCode;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @return the agent
     */
    public Agent getAgent() {
        return agent;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param discountType the discountType to set
     */
    public void setDiscountType(DiscountType discountType) {
        this.discountType = discountType;
    }

    /**
     * @param discountOffer the discountOffer to set
     */
    public void setDiscountOffer(Integer discountOffer) {
        this.discountOffer = discountOffer;
    }

    /**
     * @param commission the commission to set
     */
    public void setCommission(Integer commission) {
        this.commission = commission;
    }

    /**
     * @param agentDiscount the agentDiscount to set
     */
    public void setAgentDiscount(Integer agentDiscount) {
        this.agentDiscount = agentDiscount;
    }

    /**
     * @param userDiscount the userDiscount to set
     */
    public void setUserDiscount(Integer userDiscount) {
        this.userDiscount = userDiscount;
    }

    /**
     * @param commissionPercent the commissionPercent to set
     */
    public void setCommissionPercent(Integer commissionPercent) {
        this.commissionPercent = commissionPercent;
    }

    /**
     * @param discountCode the discountCode to set
     */
    public void setDiscountCode(String discountCode) {
        this.discountCode = discountCode;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * @param agent the agent to set
     */
    public void setAgent(Agent agent) {
        this.agent = agent;
    }
    
}
